﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using AttributeRouting;
using AttributeRouting.Web.Mvc;
using PH.Model.Schools;
using PH.Logic.Schools;
using PH.Model.Results;
using Newtonsoft.Json.Linq;
using PH.Model.Commons;
using PH.Logic.Commons;
namespace PH.Web.Management.Areas.School.Controllers
{
    [RouteArea("School")]
    [RoutePrefix("Teacher")]
    public class TeacherApiController : ApiController
    {
        [Route("GetTeachers")]
        [HttpPost]
        public JsonEntitySet<TeacherModel> GetTeachers(JObject request)
        {
            string errinfo = "no data";
            try
            {
                TeacherQuery search = new TeacherQuery();
                search.PageIndex = request.Value<int>("pageIndex");
                search.PageSize = request.Value<int>("pageSize");
                var schGuid = request.Value<string>("schoolGuid");
                if (!string.IsNullOrEmpty(schGuid))
                    search.SchoolGuid = new Guid(schGuid);
                search.Key = request.Value<string>("key");
                var result = TeacherLogic.GetTeachers(search);
                return new JsonEntitySet<TeacherModel>(result);
            }
            catch (Utils.PhException pex) { errinfo = pex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<TeacherModel>(null, errinfo, false);
        }
        [Route("SaveTeacher")]
        [HttpPost]
        public JsonEntity<TeacherModel> SaveTeacher(JObject request)
        {
            string errinfo = "对不起！保存失败，出错原因：";
            try
            {
                #region 收集参数
                TeacherModel tea = new TeacherModel();
                var guid = request.Value<string>("guid");
                tea.Guid = new Guid(guid);
                tea.ID = request.Value<int>("teaID");
                tea.SchoolID = request.Value<int>("schoolID");
                tea.RealName = request.Value<string>("realName");
                tea.Mobile = request.Value<string>("mobile");
                #endregion

                if (tea.Guid == Guid.Empty)
                    tea = TeacherLogic.CreateNewTeacher(tea);
                else
                    TeacherLogic.UpdateTeacher(tea);
                return new JsonEntity<TeacherModel>(tea);
            }
            catch (Utils.PhException pex) { errinfo = pex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntity<TeacherModel>(null, errinfo, false);
        }
        [Route("DeleteTeacher")]
        [HttpPost]
        public JsonResult<bool> DeleteTeacher(JObject request)
        {
            string errinfo = "对不起！删除失败，出错原因：";
            try
            {
                #region 收集参数
                var guid = request.Value<string>("guid");
                var teaGuid = Guid.Empty;
                if (!string.IsNullOrEmpty(guid))
                    teaGuid = new Guid(guid);

                #endregion
                TeacherLogic.DeleteTeacher(teaGuid);
                return new JsonResult<bool>(true);
            }
            catch (Utils.PhException pex) { errinfo = pex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }
    }
}
